package com.example.administrator.liveapp.activity;

import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.bean.LiveDetailsBean;
import com.example.administrator.liveapp.utils.AppUrl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import static com.example.administrator.liveapp.R.id.submit;

/**
 * 二级评论activity
 */

public class SecondaryComments extends AppCompatActivity implements View.OnClickListener{

    private EditText comment;
    private Button submit;
    private TextView name;
    private ImageView secondary_return;
    private String position;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary_comments);
        initView();
    }

    private void initView() {
        //设置各个控件的点击响应
        comment = (EditText) findViewById(R.id.comment);
        submit = (Button) findViewById(R.id.submit);
        name = (TextView) findViewById(R.id.name);
        secondary_return = (ImageView) findViewById(R.id.secondary_return);
        submit.setOnClickListener(this);
        secondary_return.setOnClickListener(this);
        Intent intent = getIntent();
        name.setText("回复"+intent.getStringExtra("name")+"评论");
        position = intent.getStringExtra("position");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.submit:
                if (TextUtils.isEmpty(comment.getText())) {
                    Toast.makeText(SecondaryComments.this, "评论不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    submit.setEnabled(false);
                    String url = AppUrl.URLS + AppUrl.CommentariesURL;
                    Map<String, String> map = new HashMap<>();
                    map.put("commentUid", ApplicationUtils.getInstance().loginBean.getConsumerid());
                    map.put("commentFid", position);
                    map.put("commentTid", "3");
                    map.put("commentContent", comment.getText().toString());
                    XUtils.Get(url, map, new MyCallBack<String>() {
                        @Override
                        public void onSuccess(String result) {
                            super.onSuccess(result);
                            try {
                                JSONObject json = new JSONObject(result);
                                String results = json.optString("result");
                                String message = json.optString("message");
                                if (results.equals("true")) {
                                    finish();
                                    Toast.makeText(SecondaryComments.this, "评论成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(SecondaryComments.this, message+"评论失败"+position, Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            super.onError(ex, isOnCallback);
                        }

                        @Override
                        public void onFinished() {
                            super.onFinished();
                            submit.setEnabled(true);
                        }
                    });
                }
                break;
            case R.id.secondary_return:
                finish();
                break;
        }
    }
}
